package com.wisdudu.blank.ui.user;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.orhanobut.logger.Logger;
import com.wisdudu.blank.R;
import com.wisdudu.blank.databinding.FragmentUserinfoUpdateBinding;
import com.wisdudu.frame.base.BaseFragment;
import com.wisdudu.blank.support.rxbus.RxBusFlag;

import java.util.ArrayList;

import static android.app.Activity.RESULT_OK;

/**
 * Created by ypp on 2016/8/16.
 */
public class UserInfoUpdateFragment extends BaseFragment {
    private static final String TAG = "UserInfoUpdateFragment";
    private UserInfoUpdateVM viewModel;
    public  static final int REQUEST_SELECT_BIRTHDAY = 100;
    public static  final int REQUEST_SELECT_IMAGE = 102;

    @Override
    protected View initBinding(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        FragmentUserinfoUpdateBinding mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_userinfo_update, container, false);
        viewModel = new UserInfoUpdateVM(this);
        mBinding.setViewModel(viewModel);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initToolbar(getToolbar(), "个人资料");
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
//        if (isVisibleToUser == true) {
//            viewModel.loadCacheUser();
//        }
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null)
            switch (requestCode) {
                case REQUEST_SELECT_BIRTHDAY://生日
                    viewModel.setBirthday(data.getStringExtra(RxBusFlag.SELECT_YMD));
                    break;
                case REQUEST_SELECT_IMAGE://头像
                    if (resultCode == RESULT_OK) {
                        ArrayList<String> stringExtra = data.getStringArrayListExtra(RxBusFlag.SELECT_IMGS);
                        Logger.i( "onActivityResult: 获取背景图片成功,%s" + stringExtra);
                        viewModel.editAvatar(stringExtra.get(0));
                    }
                    break;
            }
    }
}
